<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 站点管理员相关Controller基础类 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\controller\site;

use app\core\model\SiteAdmin;
use think\Session;

class UserController extends SiteBaseController {
    protected function _checkInitSiteAdminLogin() {
        switch ($this->request->action()) {
            case 'login':
                return "";
            default:
                return parent::_checkInitSiteAdminLogin();
        }
    }

    /**
     * 登录页面
     *
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function login() {
        $this->_siteAdminLoginRestore();

        if (is_submit()) {
            $username = input('post.username');
            $password = input('post.password');
            $this->_checkCaptcha();

            $siteAdmin = SiteAdmin::login($username, $password);
            if (!$siteAdmin) {
                $this->error("登录失败");
            }
            $this->_setSiteAdminSession($siteAdmin);
            $this->_siteAdminLoginRestore();
        } else {
            return view();
        }
    }

    /**
     * 退出登录
     */
    public function logout() {
        $this->_clearSiteAdminSession();
        return redirect(fetch($_SERVER, 'HTTP_REFERER', '/core/site/login'));
    }

    /**
     * 用户信息修改页面
     *
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function profile() {
        if (is_submit()) {
            $avatar = input("post.avatar");
            $password = input("post.password");
            $newPassword = input("post.newPassword");
            $newPassword2 = input("post.newPassword2");


            $where = ['id' => $this->siteAdminId];
            $data = [];
            //修改密码
            if (!empty($newPassword)) {
                if (empty($password)) {
                    $this->error("请输入现有登录密码");
                }
                if ($newPassword != $newPassword2) {
                    $this->error("两次输入的密码不同");
                }
                if (strlen($newPassword) < 5) {
                    $this->error("密码长度过短");
                }
                $ret = SiteAdmin::checkPassword($this->siteAdminId, $password);
                if (!$ret) {
                    $this->error("当前密码输入错误");
                }

                $data['password'] = SiteAdmin::generatePassword($newPassword);
            }
            // 头像
            if (!empty($avatar)) {
                $data['avatar'] = $avatar;
            }

            if (empty($data)) {
                $this->error("无内容更新");
            }

            SiteAdmin::update($data, $where);
            if (isset($data['password'])) {
                // 本次更新了密码，退出重新更新
                $this->_clearSiteAdminSession();
                $this->success("更新成功，请重新登录");
            } else {
                // 更新session
                $this->siteAdmin['avatar'] = $avatar;
                $this->_setSiteAdminSession($this->siteAdmin);
                $this->success("更新成功");
            }
        }
        return view();
    }
}
